<?php

// 載入圖片函式庫
require_once( 'phpthumb/ThumbLib.inc.php' );

function fn_ClientUploadThumb( $files, $folder, $name, $width, $height )
{
	$thumb = PhpThumbFactory::create($files['tmp_name']);
	$sExtension = strtolower( fn_get_file_ext($files['name']) );
	$DIR_PATH = $folder."/";
	fn_mkdir(DIR_TMP.$DIR_PATH);
	$thumb->save(DIR_TMP.$DIR_PATH.$name.".".$sExtension);
	$img['file'] = $name;
	$img['img'] = $DIR_PATH.$name.".".$sExtension;
	$destImg = fn_calculateImageSize($files, 165);
	$img['thumb'] = fn_ResizeThumbnail($files, $DIR_PATH, $name, $destImg['width'], $destImg['height']);
	return $img;
}

function fn_upload_tmp_img( $files, $folder, $name, $width, $height )
{
	$thumb = PhpThumbFactory::create($files['tmp_name']);
	$sExtension = strtolower( fn_get_file_ext($files['name']) );
	$DIR_PATH = $folder."/";
	fn_mkdir(DIR_TMP.$DIR_PATH);
	$thumb->save(DIR_TMP.$DIR_PATH.$name.".".$sExtension);
	$img['file'] = $name;
	$img['img'] = $DIR_PATH.$name.".".$sExtension;
	$img['thumb'] = fn_create_thumbnail($files, $DIR_PATH, $name, $width, $height);
	$img['thumb2'] = fn_create_thumbnail($files, $DIR_PATH, $name, 230, 230);
	return $img;
}

function fn_photo_thumber( $files, $folder, $name, $width, $height )
{
	$thumb = PhpThumbFactory::create($files['tmp_name']);
	$sExtension = strtolower( fn_get_file_ext($files['name']) );
	$DIR_PATH = $folder."/";
	fn_mkdir(DIR_TMP.$DIR_PATH);
	$thumb->save(DIR_TMP.$DIR_PATH.$name.".".$sExtension);
	$img['file'] = $name;
	$img['img'] = $DIR_PATH.$name.".".$sExtension;
	$img['thumb'] = fn_create_thumbnail($files, $DIR_PATH, $name, $width, $height);
	return $img['thumb'];
}

function fn_member_thumber( $files, $folder, $name )
{
	$thumb = PhpThumbFactory::create($files['tmp_name']);
	$sExtension = strtolower( fn_get_file_ext($files['name']) );
	$DIR_PATH = $folder."/";
	fn_mkdir(DIR_TMP.$DIR_PATH);
	//$thumb->save(DIR_TMP.$DIR_PATH.$name.".".$sExtension);
	//$img['file'] = $name;
	//$img['img'] = $DIR_PATH.$name.".".$sExtension;
	$img['photo1'] = fn_create_thumbnail($files, $DIR_PATH, $name, 164, 220);
	$img['photo2'] = fn_create_thumbnail($files, $DIR_PATH, $name, 100, 134);
	return $img;
}
function fn_user_thumber( $files, $folder, $name )
{
	$thumb = PhpThumbFactory::create($files['tmp_name']);
	$sExtension = strtolower( fn_get_file_ext($files['name']) );
	$DIR_PATH = $folder."/";
	fn_mkdir(DIR_TMP.$DIR_PATH);
	$img = fn_create_thumbnail($files, $DIR_PATH, $name, 164, 220);
	return $img;
}

function fn_album_thumber( $files, $folder, $name )
{
	$thumb = PhpThumbFactory::create($files['tmp_name']);
	$sExtension = strtolower( fn_get_file_ext($files['name']) );
	$DIR_PATH = $folder."/";
	fn_mkdir(DIR_TMP.$DIR_PATH);
	//$thumb->save(DIR_TMP.$DIR_PATH.$name.".".$sExtension);
	//$img['file'] = $name;
	//$img['img'] = $DIR_PATH.$name.".".$sExtension;
	$img['photo1'] = fn_create_thumbnail($files, $DIR_PATH, $name, 544, 408);
	$img['photo2'] = fn_create_thumbnail($files, $DIR_PATH, $name, 90, 68);
	return $img;
}
function fn_create_thumbnail( $files, $folder, $name, $width, $height )
{
	$thumb = PhpThumbFactory::create($files['tmp_name']);
	$thumb->adaptiveResize($width, $height);
	$sExtension = strtolower( fn_get_file_ext($files['name']) );
	if( $folder )
	{
		$thumb->save(DIR_TMP.$folder.$name."-".$width."x".$height.".".$sExtension);
		return $folder.$name."-".$width."x".$height.".".$sExtension;
	}
	else
	{
		$thumb->save(DIR_TMP.$name."-".$width."x".$height.".".$sExtension);
		return $name."-".$width."x".$height.".".$sExtension;
	}
}

function fn_ResizeThumbnail( $files, $folder, $name, $width, $height )
{
	$thumb = PhpThumbFactory::create($files['tmp_name']);
	$thumb->resize($width, $height);
	$sExtension = strtolower( fn_get_file_ext($files['name']) );
	if( $folder )
	{
		$thumb->save(DIR_TMP.$folder.$name."-".$width."x".$height.".".$sExtension);
		return $folder.$name."-".$width."x".$height.".".$sExtension;
	}
	else
	{
		$thumb->save(DIR_TMP.$name."-".$width."x".$height.".".$sExtension);
		return $name."-".$width."x".$height.".".$sExtension;
	}
}

function fn_clone_upload( $files, $folder, $name )
{
	$DIR_PATH = "$folder/";
	fn_mkdir(DIR_TMP.$DIR_PATH);
	$sExtension = strtolower( fn_get_file_ext($files['name']) );
	move_uploaded_file($files['tmp_name'], DIR_TMP.$DIR_PATH.$name.".".$sExtension);
	return $DIR_PATH.$name.".".$sExtension;
}

function fn_calculateImageSize( $files, $maxWidth = null, $maxHeight = null)
{
	$imageInfo = getimagesize($files['tmp_name']);
	$_sourceImageWidth  = intval($imageInfo[0]);
	$_sourceImageHeight = intval($imageInfo[1]);	
	$percent = 1;
	if( !is_null($maxWidth) )
	{
			$percent = $maxWidth/$_sourceImageWidth;
			$destImage['width']		= $maxWidth;
			$destImage['height']	= intval($_sourceImageHeight * $percent);
	}
	else
	{
			$percent = $maxHeight/$_sourceImageHeight;
			$destImage['width']		= intval($_sourceImageWidth * $percent);
			$destImage['height']	= $maxHeight;
	}	
	return $destImage;
}
?>